using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.Linq.Extensions;
using Abp.Domain.Repositories;
using Microsoft.EntityFrameworkCore;
using Sunlit.House.Books.Dto;
using Abp.Extensions;
using Sunlit.House.Authorization;

namespace Sunlit.House.Books
{
    public class BookAppService: AsyncCrudAppService<Book,BookDto,long,GetAllBookInput,
        CreateBookInput,UpdateBookInput>,IBookAppService
    {
        public BookAppService(IRepository<Book,long> repository):base(repository)
        {
        }
        protected override IQueryable<Book> CreateFilteredQuery(GetAllBookInput input)
        {
            return base.CreateFilteredQuery(input)
                .Where(b => b.Readed == input.Readed)
                .WhereIf(!input.LinkMan.IsNullOrWhiteSpace(), b => b.LinkMan.Contains(input.LinkMan))
                .WhereIf(!input.PhoneNumber.IsNullOrWhiteSpace(), b => b.PhoneNumber == input.PhoneNumber);
        }
        public async Task SetReadStateAsync(SetReadStateInput input)
        {
            var book = await Repository.GetAsync(input.Id);
            book.Readed = input.State;
        }
    }
}
